﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoreManagementSystem.DataAccessLayer;
using StoreManagementSystem.DomainObjects;
using System.Drawing;

namespace StoreManagementSystem.Login
{
    public partial class Default : System.Web.UI.Page
    {
        DataAccessLayer.SQLDAOFactory doaFactory;
        StoreUser loggeduser;

        protected void Page_Load(object sender, EventArgs e)
        {
            doaFactory = new DataAccessLayer.SQLDAOFactory(new DataAccessLayer.SQLDataProvider("MySQLConnectionString"));
            if (Session["loggeduser"] == null)
                multiviewLogin.SetActiveView(anon);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            loggeduser = Login(tbLoginName.Text, tbPassword.Text);
           
            if (loggeduser != null)
            {
                Session["loggeduser"] = loggeduser;
                Response.Redirect("~/");
            }
            else
            {
                displayMessage("Login unsuccessful.", Color.Red);
                Session["loggeduser"] = null;
            }
        }

        private void displayMessage(string message, Color color)
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = color;
            lblMessage.Text = message;
        }

        private StoreUser Login(string loginname, string password)
        {
            IDataAccessObject<DomainObjects.Entity> DAO = doaFactory.createDAO("storeuser");
            SQLStoreUserDAO userDAO = (SQLStoreUserDAO)DAO;
            StoreUser user = userDAO.getUser(loginname, password);
            
            return user;
        }
    }
}